package com.rk.jarjuggler.gui;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import com.intellij.ui.DocumentAdapter;

public class SearchPanel extends JPanel {
    private JTextField searchText;
    private JCheckBox filterSrcCheck;
    private JCheckBox filterJarCheck;
    private JCheckBox filterJavadocCheck;

    private DirSearchParams searchParams;

    public SearchPanel(DirSearchParams searchParams) {
        this.searchParams = searchParams;

        setLayout(new FlowLayout());
        layoutComponents();
        wireComponents();
    }

    private void layoutComponents() {
        searchText = new JTextField();
        searchText.setPreferredSize(new Dimension(200, (int) searchText.getPreferredSize().getHeight()));
        filterSrcCheck = new JCheckBox("Src");
        filterJarCheck = new JCheckBox("Jar");
        filterJavadocCheck = new JCheckBox("Javadoc");

        add(new JLabel("Search"));
        add(searchText);
        add(filterJarCheck);
        add(filterSrcCheck);
        add(filterJavadocCheck);
    }

    private void wireComponents(){
        searchText.getDocument().addDocumentListener(new DocumentAdapter() {
            protected void textChanged(DocumentEvent e) {
                searchParams.setSearchText(searchText.getText());
            }
        });

        CheckboxItemListener checkboxListener = new CheckboxItemListener();
        filterJarCheck.addItemListener(checkboxListener);
        filterSrcCheck.addItemListener(checkboxListener);
        filterJavadocCheck.addItemListener(checkboxListener);
    }

    class CheckboxItemListener implements ItemListener {

        public void itemStateChanged(ItemEvent itemEvent) {
            searchParams.setHasJar(filterJarCheck.isSelected());
            searchParams.setHasSource(filterSrcCheck.isSelected());
            searchParams.setHasJavadoc(filterJavadocCheck.isSelected());
        }
    }
}
